package com.lengmou.service.impl;

import com.lengmou.dao.MesBoxDao;
import com.lengmou.entity.MesBox;
import com.lengmou.service.MesBoxService;
import com.lengmou.util.TimeTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
@Service("MesBoxService")
public class MesBoxServiceImpl implements MesBoxService {
    @Resource
    private MesBoxDao mes;
    @Override
    public List<MesBox> getAll() {
        return mes.querysAll();
    }

    @Override
    public List<MesBox> getUserFromMes(int userid) {
        return mes.querysByfromUserId(userid);
    }

    @Override
    public List<MesBox> getUserAcceptMes(int userid) {
        return mes.querysByacceptId(userid);
    }

    @Override
    public List<MesBox> getUsersMes(int fromid, int acceptid) {
        return mes.querysByUsers(fromid, acceptid);
    }

    @Override
    public List<MesBox> getUserMesRead(int userid) {
        return mes.querysByUsersState(userid,1);
    }

    @Override
    public List<MesBox> getUserMesNotRead(int userid) {
        return mes.querysByUsersState(userid,0);
    }

    @Override
    public MesBox getMesBoxById(String id) {
        return mes.querysById(id);
    }

    @Override
    public int addMesBox(MesBox mesBox) {
//        初始化属性
        mesBox.setId(UUID.randomUUID().toString());
        mesBox.setState(0);
        mesBox.setCreatetime(TimeTool.time());
        return mes.addMesBox(mesBox);
    }

    @Override
    public int SetMesFromid(MesBox mesBox) {
        MesBox mesBoxById = getMesBoxById(mesBox.getId());
        if(mesBoxById!=null){
            mesBoxById.setFromid(mesBox.getFromid());
            return mes.updateMesBox(mesBoxById);
        }
        return -1;
    }

    @Override
    public int SetMesAcceptid(MesBox mesBox) {
        MesBox mesBoxById = getMesBoxById(mesBox.getId());
        if(mesBoxById!=null){
            mesBoxById.setAcceptid(mesBox.getAcceptid());
            return mes.updateMesBox(mesBoxById);
        }
        return -1;
    }

    @Override
    public int SetMesTitle(MesBox mesBox) {
        MesBox mesBoxById = getMesBoxById(mesBox.getId());
        if(mesBoxById!=null){
            mesBoxById.setTitle(mesBox.getTitle());
            return mes.updateMesBox(mesBoxById);
        }
        return -1;
    }

    @Override
    public int setMesContent(MesBox mesBox) {
        MesBox mesBoxById = getMesBoxById(mesBox.getId());
        if(mesBoxById!=null){
            mesBoxById.setContent(mesBox.getContent());
            return mes.updateMesBox(mesBoxById);
        }
        return -1;
    }

    @Override
    public int setMesState(MesBox mesBox) {
        MesBox mesBoxById = getMesBoxById(mesBox.getId());
        if(mesBoxById!=null){
            mesBoxById.setState(mesBox.getState());
            return mes.updateMesBox(mesBoxById);
        }
        return 0;
    }

    @Override
    public int setMesTime(MesBox mesBox) {
        MesBox mesBoxById = getMesBoxById(mesBox.getId());
        if(mesBoxById!=null){
            mesBoxById.setCreatetime(mesBox.getCreatetime());
            return mes.updateMesBox(mesBoxById);
        }
        return -1;
    }

    @Override
    public int deleteMes(String id) {
        MesBox mesBoxById = getMesBoxById(id);
        if(mesBoxById!=null){
            return mes.deleteMesBox(id);
        }
        return -1;
    }
}
